﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using Bll;
using System.Data;
using System.Web.Services;
using System.Text.RegularExpressions;

namespace Accounting.admin.navigation
{
    public partial class NavSub : System.Web.UI.Page
    {
        bllNavigation b = new bllNavigation();
        ModelPublic m = new ModelPublic();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                NavBind();
                NotNav.Visible = true;
                ShowNav.Visible = false;
                btnDel.Visible = false;
                DivCboxShow.Visible = false;
            }
        }

        private void NavBind()
        {
            DataTable dt = b.Nav_Name_All();

            RpNav.DataSource = dt;
            RpNav.DataBind();

            foreach (DataRow my in dt.Rows)
            {
                string id=my["Id"].ToString();
                string name=my["Name"].ToString();
                if (id != "1")
                {
                    ddlNav.Items.Add(new ListItem(name, id));
                }
            }
        }


        private void SubNavBind()
        {
            if (m.Id == "1")
            {
                NotNav.Visible = true;
                ShowNav.Visible = false;
            }
            else
            {
                NotNav.Visible = false;
                ShowNav.Visible = true;
            }

            m.NavId = lblNavId.Text;
            RpSubNav.DataSource = b.SubNavContent(m);
            RpSubNav.DataBind();
        }

        protected void RpNav_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Label lblId = (Label)e.Item.FindControl("lblId");
            

            if (lblId.Text != "1")
            {
                ddlNav.SelectedValue = lblId.Text;
                lblNavId.Text = lblId.Text;
            }

            LinkButton lBtn = (LinkButton)e.Item.FindControl("LbtnNavName");

            DivTitle.InnerText = "导航名称："+ lBtn.Text;
            
            SubNavBind();

            if (m.Id == "3")
                DivCboxShow.Visible = true;
            else
                DivCboxShow.Visible = false;
            
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("./NavSub.aspx");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            bool Is_Web = false;
            if (txtWeb.Text != "")
            {
                Is_Web = Regex.IsMatch(m.Web, @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?$",
                    RegexOptions.IgnoreCase);
                m.Web = "'" + txtWeb.Text.Trim() + "'";
            }
            else
                m.Web = "NULL";

            m.Name = "'" + txtName.Text.Trim() + "'";
            //if (txtEnglishName.Text == "")
            //    m.EnglishName = "NULL";
            //else
            //    m.EnglishName = "'" + txtEnglishName.Text.Trim() + "'";
            m.Sort = "'" + txtSort.Text.Trim() + "'";
            m.BelongTo = "'" + ddlNav.SelectedValue + "'";
            if (CboxShow.Checked == true)
                m.Show = "'true'";
            else
                m.Show = "'False'";

            if(lblSubId.Text=="")
            {
                b.SubNav_Add(m);
            }
            else
            {
                m.SqlId = "2";
                m.Id = lblSubId.Text;
                b.Update_Information(m);
            }
            SubNavBind();
            DivTitle.InnerText = "导航名称：" + ddlNav.SelectedItem.Text;
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            m.Id = lblSubId.Text;

            b.Data_Delete(m);

            m.Id = ddlNav.SelectedValue;
            SubNavBind();

            ContentClear();

        }

        private void ContentClear()
        {
            ddlNav.SelectedIndex = -1;
            //txtEnglishName.Text = "";
            txtName.Text = "";
            txtSort.Text = "";
            lblNavId.Text="";
            lblSubId.Text="";
            txtWeb.Text = "";
        }

        protected void RpSubNav_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            btnDel.Visible = true;

            //Label NavId = (Label)e.Item.FindControl("lblNavId");
            Label SubId = (Label)e.Item.FindControl("lblSubId");
            Label NavName = (Label)e.Item.FindControl("lblName");
            Label NavSort = (Label)e.Item.FindControl("lblSort");
            Label English = (Label)e.Item.FindControl("lblEnglishName");
            Label Show = (Label)e.Item.FindControl("lblSubNavShow");

            txtName.Text = NavName.Text;
            txtSort.Text = NavSort.Text;
            
            lblSubId.Text = SubId.Text;
            //txtEnglishName.Text = English.Text;
            if (Show.Text == "True")
                CboxShow.Checked = true;
            else
                CboxShow.Checked = false;
        }
    }
}